<?php

/**
 * 餐厅编辑post
 * 
 * @copyright xinwei
 * @since 2012-04-17
 */


include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");

include_once($_SERVER["DOCUMENT_ROOT"] . "/include/user/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/mblog/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/tuijian/inc.php");
$g_cgival = array();
$g_pro = array();
$g_show = array();

function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	//check refer  -  add by xinwei
	if(! security_check::check_refer())
	{
		throw new Exception("来路不明");
	}
	
	
	cgi::both($g_cgival["id"], "id", 0);
	$g_cgival["id"] = intval($g_cgival["id"]);
	
	
	cgi::both($g_cgival["title1"], "title1", "");
	$g_cgival["title1"] = string::un_html($g_cgival["title1"]);
	
	cgi::both($g_cgival["title2"], "title2", "");
	$g_cgival["title2"] = string::un_html($g_cgival["title2"]);
	
	cgi::both($g_cgival["link"], "link", "");
	$g_cgival["link"] = string::un_html($g_cgival["link"]);
	
	cgi::both($g_cgival["type"], "type", "add");

	
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show,$config;
	
	$id = $g_cgival["id"];	
	$title1 = $g_cgival["title1"];
	$title2 = $g_cgival["title2"];
	$link = $g_cgival["link"];
	
	
	
	if($g_cgival["type"] == "add")
	{
		if($id)
		{
			if($_FILES["pic1"]["name"])
			{
				$upload_config=array(
				'targetDir' => 'tuijian_jiaodian',//文件保存路径
				'saveType' => 2, //保存目录结构，默认两级
				'thumbStatus'=> 1, //开启图片压缩，1为等比缩放，2为裁剪
				'thumbSize' => array("100|100"), //压缩文件尺寸限制，原图为xxx.jpg压缩文件名为xxx_100.jpg(100为你填写的宽度限制值)
				'allowMaxSize' => 1024*1024*3, //允许上传图片大小
				'allowExtensions' => array('jpg', 'jpeg', 'gif', 'png', 'bmp')//允许上传的图片格式
				);
				
				$upload = new pic_upload($upload_config);
				$files = $upload->saveFiles("pic1");//传入表单图片名
				
				$pic1 = $files[0]["pic_return_info"]["pic_cut"][0];
				
			}
		
			
			if($_FILES["pic2"]["name"])
			{
				$upload_config=array(
				'targetDir' => 'tuijian_jiaodian',//文件保存路径
				'saveType' => 2, //保存目录结构，默认两级
				'thumbStatus'=> 1, //开启图片压缩，1为等比缩放，2为裁剪
				'thumbSize' => array("500|500"), //压缩文件尺寸限制，原图为xxx.jpg压缩文件名为xxx_100.jpg(100为你填写的宽度限制值)
				'allowMaxSize' => 1024*1024*3, //允许上传图片大小
				'allowExtensions' => array('jpg', 'jpeg', 'gif', 'png', 'bmp')//允许上传的图片格式
				);
				$upload = new pic_upload($upload_config);
				
				
				$files = $upload->saveFiles("pic2");//传入表单图片名
				$pic2 = $files[0]["pic_return_info"]["pic_cut"][0];
				
			}
			$db_tuijian_jiaodian = new db_tuijian_jiaodian();
			
			$db_tuijian_jiaodian->admin_update_by_id($id,"",$pic1,$pic2,$title1,$title2,$link);
			echo "<script>alert('操作成功');location.href='tuijian_jiaodian_list.php';</script>";
		}
		else
		{
			
			$upload_config=array(
			'targetDir' => 'tuijian_jiaodian',//文件保存路径
			'saveType' => 2, //保存目录结构，默认两级
			'thumbStatus'=> 1, //开启图片压缩，1为等比缩放，2为裁剪
			'thumbSize' => array("50|50"), //压缩文件尺寸限制，原图为xxx.jpg压缩文件名为xxx_100.jpg(100为你填写的宽度限制值)
			'allowMaxSize' => 1024*1024*3, //允许上传图片大小
			'allowExtensions' => array('jpg', 'jpeg', 'gif', 'png', 'bmp')//允许上传的图片格式
			);
			$upload = new pic_upload($upload_config);
			$files = $upload->saveFiles("pic1");//传入表单图片名
			$pic1 = $files[0]["pic_return_info"]["pic_cut"][0];
			
			
			
			$upload_config=array(
			'targetDir' => 'tuijian_jiaodian',//文件保存路径
			'saveType' => 2, //保存目录结构，默认两级
			'thumbStatus'=> 1, //开启图片压缩，1为等比缩放，2为裁剪
			'thumbSize' => array("450|300"), //压缩文件尺寸限制，原图为xxx.jpg压缩文件名为xxx_100.jpg(100为你填写的宽度限制值)
			'allowMaxSize' => 1024*1024*3, //允许上传图片大小
			'allowExtensions' => array('jpg', 'jpeg', 'gif', 'png', 'bmp')//允许上传的图片格式
			);
			$upload = new pic_upload($upload_config);
			$files = $upload->saveFiles("pic2");//传入表单图片名
			$pic2 = $files[0]["pic_return_info"]["pic_cut"][0];
			
			
			if(!$pic1 || !$pic2 || !$title1 || !$title2 || !$link)
			{
				echo "参数错误！";
				exit;
			}
			$db_tuijian_jiaodian = new db_tuijian_jiaodian();
			
			$res = $db_tuijian_jiaodian->admin_insert_tuijian_jiaodian($pic1,$pic2,$title1,$title2,$link);
			if(!$res)
			{
				echo "数据库操作失败！";
				exit;
			}
			
			echo "<script>alert('操作成功');location.href='tuijian_jiaodian_list.php';</script>";
		
		}
			
	}
	elseif($g_cgival["type"] == "del")
	{
		$db_tuijian_jiaodian = new db_tuijian_jiaodian();
		$res = $db_tuijian_jiaodian->admin_update_by_id($id,0);
		if(!$res)
		{
			throw new Exception("数据库操作失败！");
		}
	}
	elseif($g_cgival["type"] == "undel")
	{
		$db_tuijian_jiaodian = new db_tuijian_jiaodian();
		$res = $db_tuijian_jiaodian->admin_update_by_id($id,1);
		if(!$res)
		{
			throw new Exception("数据库操作失败！");
		}
	}
}

$g_show["error"] = 0;
$g_show["errmsg"] = '';
try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/album_list.php");
	
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if(isset($_GET['debug']))
	{
		print_r($g_show);
		exit;
	}
	
	
	echo json_encode($g_show);
}

